//
//  YPStationMessageViewController.m
//  ChengHuiTong
//
//  Created by KiVen on 15/11/24.
//  Copyright © 2015年 张文权. All rights reserved.
//

#import "YPStationMessageViewController.h"
#import "YPMessageTableViewCell.h"
#import "YPStationMessageModel.h"
#import "YPMessageDetailViewController.h"
#import "MainTabbarViewController.h"


#import "SWTableViewCell.h"
@interface YPStationMessageViewController ()<UITableViewDataSource,UITableViewDelegate,SWTableViewCellDelegate>{
    
    UITableView * _stationMessageTableView;
    NSMutableArray * _stationMessageArray;
    int _page;
    
}

@property(nonatomic,strong)UITableView * stationMessageTableView;

@end

@implementation YPStationMessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _page = 1;
    self.view.backgroundColor = UIColorFromRGB(0xF9F9F9);
    self.navigationItem.title = @"站内信";
    
    _stationMessageArray = [[NSMutableArray alloc]initWithCapacity:0];
    
    [self creatStationMessageView];
    
    
}
- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:YES];;
    
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    
    [MobClick beginLogPageView:@"站内信"];
    
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:YES];
    [MobClick endLogPageView:@"站内信"];
    
    if (self.navigationController.viewControllers.count > 1) {
        [[NSNotificationCenter defaultCenter]postNotificationName:@"YPAssetsVCRefreshNofification" object:nil];
    }
    
}

- (void)creatStationMessageView{
    
    _stationMessageTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStylePlain];
    _stationMessageTableView.dataSource = self;
    _stationMessageTableView.delegate = self;
    _stationMessageTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    WeakSelf
    [_stationMessageTableView addGifHeaderWithRefreshingBlock:^{
        
        [weakSelf requestHeader];
    }];
    
    _stationMessageTableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [weakSelf requestFooter];
        
    }];
    

    [_stationMessageTableView.header beginRefreshing];
    
    [self.view addSubview:_stationMessageTableView];
    
}

#pragma mark - refresh
//下拉刷新
- (void)requestHeader {
    
    _page = 1;
    
    YPStationMessageBin * bin = [[YPStationMessageBin alloc]init];
    
    bin.token = [YPShareAccountInfo shareAccountInfo].token;
    
    bin.page = 1;
    
    [YPHttpRequestMethod sendAsyRequestURL:CMD_STATION_MESSAGE withData:[bin dictionaryValue] withCompletionBlock:^(NSDictionary *responseDictionary) {
        NSLog(@"responseDictionary=%@",responseDictionary);
        
        if ([responseDictionary[@"status"]  intValue]==0) {
            
           [_stationMessageArray removeAllObjects];
            
           [self configDataWithDic:responseDictionary];
        }
        
    } withFailedBlock:^(NSError *error) {
        
        [_stationMessageTableView.header endRefreshing];
        [_stationMessageTableView.footer endRefreshing];
        
    } withIndicator:NO atView:self.view];
    
    
    
    
    
}
//上拉加载
- (void)requestFooter{
    
    _page++;
    
    YPStationMessageBin * bin = [[YPStationMessageBin alloc]init];
    
    bin.token = [YPShareAccountInfo shareAccountInfo].token;
    
    bin.page = _page;
    
    [YPHttpRequestMethod sendAsyRequestURL:CMD_STATION_MESSAGE withData:[bin dictionaryValue] withCompletionBlock:^(NSDictionary *responseDictionary) {
        
        NSLog(@"responseDictionary=%@",responseDictionary);
        
        if ([responseDictionary[@"status"] intValue]==0) {

            [self configDataWithDic:responseDictionary];

        }
        
    } withFailedBlock:^(NSError *error) {
        
        [_stationMessageTableView.header endRefreshing];
        [_stationMessageTableView.footer endRefreshing];
        
    } withIndicator:NO atView:self.view];
    
    
}

- (void)configDataWithDic:(NSDictionary *)dic{
    
    NSMutableArray * dataArray =dic[@"list"];
    [dataArray enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL *stop) {
        YPStationMessageModel * model = [[YPStationMessageModel alloc]initWithDictionary:obj];
        [_stationMessageArray addObject:model];
        
    }];
    
    [_stationMessageTableView reloadData];
    [_stationMessageTableView.header endRefreshing];
    [_stationMessageTableView.footer endRefreshing];
    
    if ( dataArray.count <10) {
        [_stationMessageTableView reloadData];
        [_stationMessageTableView.header endRefreshing];
        [_stationMessageTableView.footer endRefreshing];
        [_stationMessageTableView.footer noticeNoMoreData];
        
    }
    
    if (_stationMessageArray.count == 0) {
        
        _stationMessageTableView.tableFooterView=[SelectView footViewWithheight:_stationMessageTableView.height];
    }
}

#pragma mark -- TableViewDelegate DataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;{
    
    return _stationMessageArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 100;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    YPStationMessageModel * model = [_stationMessageArray objectAtIndex:indexPath.row];
    
    if ([model.status intValue]==0) {
        
        YPMessageTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
        
        if (!cell) {
            
            cell = [[YPMessageTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell1"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            [cell setRightUtilityButtons:[self oneButton]WithButtonWidth:100];
            
            cell.delegate=self;
        }
        
        
        [cell setContent:model];
        
        
        return cell;
        
    }else if ([model.status intValue]==1){
        
        YPMessageTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell2"];
        
        if (!cell) {
            
            cell = [[YPMessageTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell2"];
            
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            [cell setRightUtilityButtons:[self rightButtons]WithButtonWidth:100];
            
            cell.delegate=self;
        }
        
        
        
        [cell setContent:model];
        
        return cell;
    }
    
    
    
    
    return nil;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;{
    
    YPMessageDetailViewController * messageDetailVC = [[YPMessageDetailViewController alloc]init];
    YPStationMessageModel * model = [_stationMessageArray objectAtIndex:indexPath.row];
    
    //    model.status =@"1";
    messageDetailVC.midString =model.mid;
    
    YPStationMessageIsReeadBin * bin = [[YPStationMessageIsReeadBin alloc]init];
    
    bin.token = [YPShareAccountInfo shareAccountInfo].token;
    
    bin.mid = model.mid;
    
    
    model.status =@"1";
    
    
    
    
    YPMessageTableViewCell*cell=(YPMessageTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
    
    cell.badgeLabel.hidden=YES;
    
    UIButton*button=cell.rightUtilityButtons[0];
    
    [button setTitle:@"标为未读" forState:0];
    
    
    [self.navigationController pushViewController:messageDetailVC animated:YES];
    
    
    
}

-(NSArray*)oneButton{
    
    NSMutableArray *rightUtilityButtons = [NSMutableArray new];
    [rightUtilityButtons sw_addUtilityButtonWithColor:
     UIColorFromString(@"#FB9703") title:@"标为已读"];
    [rightUtilityButtons sw_addUtilityButtonWithColor:
     [UIColor colorWithRed:1.0f green:0.231f blue:0.188 alpha:1.0f]
                                                title:@"删除"];
    return rightUtilityButtons;
    
    
}
- (NSArray *)rightButtons
{
    NSMutableArray *rightUtilityButtons = [NSMutableArray new];
    [rightUtilityButtons sw_addUtilityButtonWithColor:
     UIColorFromString(@"#FB9703") title:@"标为未读"];
    [rightUtilityButtons sw_addUtilityButtonWithColor:
     [UIColor colorWithRed:1.0f green:0.231f blue:0.188 alpha:1.0f]
                                                title:@"删除"];
    
    return rightUtilityButtons;
}


#pragma mark - SWTableViewDelegate

- (void)swipeableTableViewCell:(SWTableViewCell *)cell scrollingToState:(SWCellState)state
{
    switch (state) {
        case 0:
            NSLog(@"utility buttons closed");
            break;
        case 1:
            NSLog(@"left utility buttons open");
            break;
        case 2:
            NSLog(@"right utility buttons open");
            break;
        default:
            break;
    }
}

- (void)swipeableTableViewCell:(SWTableViewCell *)cell didTriggerLeftUtilityButtonWithIndex:(NSInteger)index
{
    switch (index) {
        case 0:
            NSLog(@"left button 0 was pressed");
            break;
        case 1:
            NSLog(@"left button 1 was pressed");
            break;
        case 2:
            NSLog(@"left button 2 was pressed");
            break;
        case 3:
            NSLog(@"left btton 3 was pressed");
        default:
            break;
    }
}

- (void)swipeableTableViewCell:(SWTableViewCell *)cell didTriggerRightUtilityButtonWithIndex:(NSInteger)index
{
    
    NSIndexPath*indexPath=[_stationMessageTableView indexPathForCell:cell];
    
    YPStationMessageModel * model = [_stationMessageArray objectAtIndex:indexPath.row];
    switch (index) {
        case 0:
        {
            NSLog(@"More button was pressed");
            
            if ([model.status intValue]==0) {
                
                
                
                YPStationMessageIsReeadBin * bin = [[YPStationMessageIsReeadBin alloc]init];
                
                bin.token = [YPShareAccountInfo shareAccountInfo].token;
                
                bin.mid = model.mid;
                
                
                model.status =@"1";
                
                
                YPMessageTableViewCell*cell=(YPMessageTableViewCell*)[_stationMessageTableView cellForRowAtIndexPath:indexPath];
                
                cell.badgeLabel.hidden=YES;
                
                UIButton*button=cell.rightUtilityButtons[0];
                
                [button setTitle:@"标为未读" forState:0];
                
                [YPHttpRequestMethod sendAsyRequestURL:CMD_STATION_ISREAD withData:[bin dictionaryValue] withCompletionBlock:^(NSDictionary *responseDictionary) {
                    
                    NSLog(@"%@",responseDictionary);
                    
                } withFailedBlock:^(NSError *error) {
                    
                    
                } withIndicator:NO atView:self.view];
                
            }else if ([model.status intValue]==1){

                YPStationMessageIsReeadBin * bin = [[YPStationMessageIsReeadBin alloc]init];
                
                bin.token = [YPShareAccountInfo shareAccountInfo].token;
                
                bin.mid = model.mid;
                
                model.status =@"0";
                
                UIButton*button=cell.rightUtilityButtons[0];
                
                [button setTitle:@"标为已读" forState:0];
                
                YPMessageTableViewCell*cell=(YPMessageTableViewCell*)[_stationMessageTableView cellForRowAtIndexPath:indexPath];
                
                cell.badgeLabel.hidden=NO;
                
                [YPHttpRequestMethod sendAsyRequestURL:CMD_STATION_ISREAD withData:[bin dictionaryValue] withCompletionBlock:^(NSDictionary *responseDictionary) {
                                    
                    
                } withFailedBlock:^(NSError *error) {
                    
                    
                } withIndicator:NO atView:self.view];
                
            }
            
            [cell hideUtilityButtonsAnimated:YES];
            
            break;
        }
        case 1:
        {
            // Delete button was pressed
            
            
            
            YPStationMessageBin * bin =[[YPStationMessageBin alloc]init];
            
            bin.token = [YPShareAccountInfo shareAccountInfo].token;
            YPStationMessageModel * model = [_stationMessageArray objectAtIndex:indexPath.row];
            bin.mid =model.mid;
            
            
            [YPHttpRequestMethod sendAsyRequestURL:CMD_DELETE_STATION_MESSAGE withData:[bin dictionaryValue] withCompletionBlock:^(NSDictionary *responseDictionary) {
                
                NSLog(@"responseDictionary=%@",responseDictionary);
                
                [_stationMessageArray removeObjectAtIndex:indexPath.row];
                //动画删除一行
                
                [_stationMessageTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
                
                [_stationMessageTableView reloadData];
                                
            } withFailedBlock:^(NSError *error) {
                
                
            } withIndicator:YES atView:self.view];
                        
            
            break;
        }
        default:
            break;
    }
}

- (BOOL)swipeableTableViewCellShouldHideUtilityButtonsOnSwipe:(SWTableViewCell *)cell
{
    // allow just one cell's utility button to be open at once
    return YES;
}

- (BOOL)swipeableTableViewCell:(SWTableViewCell *)cell canSwipeToState:(SWCellState)state
{
    switch (state) {
        case 1:
            // set to NO to disable all left utility buttons appearing
            return YES;
            break;
        case 2:
            // set to NO to disable all right utility buttons appearing
            return YES;
            break;
        default:
            break;
    }
    
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
